<template>
  <div class="login-container">
    <div class="loginform">
      <img
        src="http://likede2-admin.itheima.net/img/logo.595745bd.png"
        alt=""
        class="login-img"
      >
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        auto-complete="on"
        label-position="left"
      >
        <el-form-item prop="loginName">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            ref="username"
            v-model="loginForm.loginName"
            placeholder="Username"
            name="username"
            type="text"
          />
        </el-form-item>

        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="Password"
            name="password"
            @keyup.enter.native="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon
              :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
            />
          </span>
        </el-form-item>

        <el-form-item prop="imgSrc">
          <span class="svg-container">
            <i class="el-icon-view" />
          </span>
          <el-input
            ref="img"
            v-model="loginForm.imgSrc"
            placeholder="请输入验证码"
            name="username"
            type="text"
          />
          <div class="btton" @click="getImg">
            <img :src="jb" alt="" style="height: 100%">
          </div>
        </el-form-item>
        <el-button
          class="btn"
          :loading="loading"
          type="primary"
          style="width: 100%; margin-bottom: 20px"
          @click.native.prevent="handleLogin"
        >Login</el-button>

        <!-- <div class="tips">
          <span style="margin-right:20px;">username: admin</span>
          <span> password: any</span>
        </div> -->
      </el-form>
    </div>
  </div>
</template>

<script>
// import { validUsername } from '@/utils/validate'
import { getCode } from '@/api/user'
export default {
  name: 'Login',
  data() {
    // const validateUsername = (rule, value, callback) => {
    //   if (!validUsername(value)) {
    //     callback(new Error('Please enter the correct user name'))
    //   } else {
    //     callback()
    //   }
    // }
    // const validatePassword = (rule, value, callback) => {
    //   if (value.length < 5) {
    //     callback(new Error('The password can not be less than 6 digits'))
    //   } else {
    //     callback()
    //   }
    // }
    return {
      loginForm: {
        loginName: 'admin',
        password: 'admin',
        // code: this.imgSrc,
        clientToken: this.randomToken,
        loginType: 0,
        imgSrc: ''
      },
      jb: '',
      randomToken: '',
      loginRules: {
        loginName: [
          { required: true, trigger: 'blur', message: '请输入用户名' }
        ],
        password: [
          { required: true, trigger: 'blur', message: '请输入密码' },
          { min: 1, max: 5, message: '密码为1-5位', trigger: 'blur' }
        ],
        imgSrc: [
          { required: true, trigger: 'blur', message: '必填' },
          { min: 4, max: 4, message: '四位验证码', trigger: 'blur' }
        ]
      },
      // 按钮状态
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      // 验证图片
      codeImg: '',
      // 随机数
      random: null
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  // 页面加载验证码
  async created() {
    this.random = Math.floor(Math.random() * 10)
    this.randomToken = this.random
    const res = await getCode(this.random)
    const img = window.URL.createObjectURL(res.data)
    this.jb = img
    // console.log(img)
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    // 点击验证码
    async getImg() {
      this.random = Math.floor(Math.random() * 10)
      this.loginForm.randomToken = this.random
      const res = await getCode(this.random)
      const img = window.URL.createObjectURL(res.data)
      this.jb = img
    },
    handleLogin() {
      // 兜底校验
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true
          this.$store
            .dispatch('user/loginForm', {
              loginName: 'admin',
              password: 'admin',
              code: this.loginForm.imgSrc,
              clientToken: this.randomToken,
              loginType: 0
            })
            .then(() => {
              // this.$router.push({ path: this.redirect || '/' })
              this.$router.push('/')
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
    color: $bg;
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;
      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }
  .btn {
    background: linear-gradient(262deg, #2e50e1, #6878f0);
    height: 47px;
  }
  .btton {
    position: absolute;
    top: 0;
    right: 0;
    width: 130px;
    height: 100%;
    // background-color: yellow;
  }
  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
  .login-img {
    position: absolute;
    width: 96px;
    height: 96px;
    top: -46px;
    left: 50%;
    margin-left: -48px;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  background-image: url("http://likede2-admin.itheima.net/img/background.be4fae7d.png");

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    // padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }
  .loginform {
    position: absolute;
    width: 518px;
    height: 388px;
    top: 50%;
    left: 50%;
    margin-top: -194px;
    margin-left: -259px;
    padding: 76px 35px 0;
    background: #fff;
    box-shadow: 0 3px 70px 0 rgb(30 111 72 / 35%);
    border-radius: 10px;
  }
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }
  .el-form-item__content {
    line-height: 40px;
    position: relative;
    font-size: 14px;
    color: $bg;
  }
  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
